package com.msmall.community.play.cases.dao.hibernate;

import java.util.List;

import com.msmall.base.dao.hibernate.BaseDaoHibernate;
import com.msmall.community.play.cases.dao.ScenarioDao;
import com.msmall.community.play.cases.model.Action;
import com.msmall.community.play.cases.model.Scenario;
import com.msmall.community.play.cases.model.Target;

public class ScenarioDaoHibernate extends BaseDaoHibernate<Scenario, Long> implements ScenarioDao{

	public ScenarioDaoHibernate(){
		super(Scenario.class);
	}
	/**
	 * 通过场景ID获得此场景下所有的ACTION
	 * @param scenarioId
	 * @return
	 */
	public List<Action> getActionsByScenario(Long scenarioId) throws Exception {
		String queryString = "from Action a where a.scenario.scenarioId=?";
		try{
			List list = getHibernateTemplate().find(queryString, scenarioId);
			return list;
		}catch(Exception e){
			throw e;
		}
	}
	/**
	 *  通过场景ID获得此场景下所有的TARGET
	 * @param scenarioId
	 * @return
	 */
	public List<Target> getTargetsByScenario(Long scenarioId) throws Exception {
		String queryString = "from Target t where t.scenario.scenarioId=?";
		try{
			List list = getHibernateTemplate().find(queryString, scenarioId);
			return list;
		}catch(Exception e){
			throw e;
		}
	}
	
	/**
	 * 找到此动作所有的直接结果场景
	 * @param actionId
	 * @return
	 * @throws Exception
	 */
	public List<Scenario> getByAction(Long actionId) throws Exception{
		String queryString = "from Scenario s where s.scenarioCode like '" + actionId + "-%'";
		try{
			List list = getHibernateTemplate().find(queryString);
			return list;
		}catch(Exception e){
			throw e;
		}
	}
	/**
	 * 找到此动作对象所有的直接结果场景
	 * @param targetId
	 * @return
	 * @throws Exception
	 */
	public List<Scenario> getByTarget(Long targetId) throws Exception{
		String queryString = "from Scenario s where s.scenarioCode like '%-" + targetId + "'";
		try{
			List list = getHibernateTemplate().find(queryString);
			return list;
		}catch(Exception e){
			throw e;
		}
	}
	
	/**
	 * 通过动作和对象找到唯一的直接结果场景
	 * @param actionId
	 * @param targetId
	 * @return
	 * @throws Exception
	 */
	public Scenario getByActionTarget(Long actionId,Long targetId) throws Exception{
		String queryString = "from Scenario s where s.scenarioCode = ?";
		try{
			List list = getHibernateTemplate().find(queryString, actionId+"-"+targetId);
			if(list !=null && list.size()>0)
				return (Scenario) list.get(0);
			return null;
		}catch(Exception e){
			throw e;
		}
	}
	
	public void removeScenario(Long scenarioId) throws Exception{
		try{
			Scenario scenario = (Scenario) getHibernateTemplate().load(Scenario.class, scenarioId);
			remove(scenario);
		}catch(Exception e){
			throw e;
		}
	}
}
